class Solution(object):
    def primePalindrome(self, n):
        s = str(n)
        m = len(s)
        sushu = []
        p = [0] * 20000
        p[0] = 1
        for i in range(1, 20000):
            if not p[i]:
                sushu.append(i + 1)
            for num in sushu:
                if (i + 1) * num - 1 < 20000:
                    p[(i + 1) * num - 1] = 1
                    if (i + 1) % num == 0:
                        break
                else:
                    break
        def judge(num):
            for t in sushu:
                if t < num:
                    if num % t == 0:
                        return False
                if t == num:
                    return True
            return True

        for k in range(m, 10):
            if k % 2 == 0:
                left = k // 2
                for i in range(10 ** (left - 1), 10 ** left):
                    num = int(str(i) + str(i)[::-1])
                    if num >= n and judge(num):
                        return num
            else:
                left = (k + 1) // 2
                for i in range(10 ** (left - 1), 10 ** left):
                    num = int(str(i) + str(i)[-2::-1])
                    if num >= n and judge(num):
                        return num
data = Solution()
n = 6
print(data.primePalindrome(n))